#include "stdio.h"
#include "pid.h"

#define PWM_TIM_PULSE_TPWM  (CKTIM/(PWM_FREQ))

#define PWM2_MODE 0
#define PWM1_MODE 1

typedef struct
{
    float Valpha;
    float Vbeta;
}VOLTAGE_ALPHA_BETA_DEF;

typedef struct
{
    float Vd;
    float Vq;
}VOLTAGE_DQ_DEF;

//三相电流结构体
typedef struct
{
    float Va;
    float Vb;
    float Vc;
}VOTAGE_ABC_DEF;

typedef struct
{
    float Ia;
    float Ib;
    float Ic;
}Current_ABC_DEF;

typedef struct
{
    float I_alpha;
    float I_beta;
}CURRENT_ALPHA_BETA_DEF;

typedef struct
{
    float Id;
    float Iq;
}CURRENT_DQ_DEF;

typedef struct
{
    float cos;
    float sin;
}TRANSFER_COS_SIN_DEF;

typedef struct Averge_Filter
{
    float buffer[5];
    float out;
    uint8_t idx;
} Averge_Filter;

extern float Temperature;
extern float Circle_Num;
extern int OFFSET_A;
extern int OFFSET_B;
extern uint16_t OFFSET_C;
extern int Inject_ADC_Value[4];
extern uint16_t ADC_OFFSET_A[10];
extern uint16_t ADC_OFFSET_C[10];
extern uint16_t ADC_OFFSET_B[10];
extern VOTAGE_ABC_DEF V_DATA;
extern VOLTAGE_DQ_DEF Voltage_DQ;
extern Current_ABC_DEF I_DATA;
extern CURRENT_ALPHA_BETA_DEF I_ALPHA_BETA_DATA;
extern TRANSFER_COS_SIN_DEF TRANSFER_COS_SIN;
extern CURRENT_DQ_DEF Current_DQ;
extern PID_TypeDef VD;
extern PID_TypeDef VQ;
extern PID_TypeDef Speed;
extern PID_TypeDef Position;
extern VOLTAGE_ALPHA_BETA_DEF Voltage_Alpha_Beta;
extern float Target_Position;
extern float Target_Position_Pre;
extern uint8_t Start_Flag;
extern uint16_t Count_ADC;
extern Averge_Filter AF_1, AF_2, AF_3, AF_4;

void PWM_Init();
void PID_Init();
void Get_Current_ABC();
void Clarke_Transfer(Current_ABC_DEF Current_ABC,CURRENT_ALPHA_BETA_DEF * Current_alpha_beta_temp);
void Park_Transfer(CURRENT_ALPHA_BETA_DEF Current_Alpha_Beta_Temp, TRANSFER_COS_SIN_DEF Cos_Sin_Temp,
                   CURRENT_DQ_DEF *Current_Dq_Temp);
void Angle_To_Cos_Sin(float angle_temp,TRANSFER_COS_SIN_DEF * cos_sin_temp);
void Rev_Park_Transfer(VOLTAGE_DQ_DEF V_Dq_Temp,TRANSFER_COS_SIN_DEF Cos_Sin_Temp,VOLTAGE_ALPHA_BETA_DEF * V_Alpha_Beta_Temp);
void SVPWM_Control(VOLTAGE_ALPHA_BETA_DEF V_ALPHA_BETA_TEMP,float UDC_TEMP,float TPWM_TEMP);
float Get_Motor_Speed();
void Rev_Clark_Transfer(VOLTAGE_ALPHA_BETA_DEF V_AB_TEMP,VOTAGE_ABC_DEF * V_ABC_TEMP);
void OFFSET_INIT();
void Electrical_Angle_Init();
void ADC_Injection_Init();
void AF_update(Averge_Filter * af, float data);
void AF_init(Averge_Filter * af);
float Get_Temperature();
void Temperature_Control();
